package org.smart4j.plugin.rest;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor;
import org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor;
import org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor;
import org.apache.cxf.message.Message;
import org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author djw
 * @time 2017/10/30 16:17
 */
public class RestHelper {
    private static final List<Object> providerList=new ArrayList<>();
    private static final List<Interceptor<? extends Message>> inInterceptorList=new ArrayList<>();
    private static final List<Interceptor<? extends Message>> outInterceptorList=new ArrayList<>();

    static {
        JacksonJsonProvider jsonProvider=new JacksonJsonProvider();
        providerList.add(jsonProvider);
//        if(RestConfig.isLog()){
//
//        }
        if(RestConfig.isJsonp()){
            JsonpInInterceptor jsonpInInterceptor=new JsonpInInterceptor();
            jsonpInInterceptor.setCallbackParam(RestConfig.getJsonpFunction());
            inInterceptorList.add(jsonpInInterceptor);
            JsonpPreStreamInterceptor jsonpPreStreamInterceptor=new JsonpPreStreamInterceptor();
            outInterceptorList.add(jsonpPreStreamInterceptor);
            JsonpPostStreamInterceptor jsonpPostStreamInterceptor=new JsonpPostStreamInterceptor();
            outInterceptorList.add(jsonpPostStreamInterceptor);
        }
        if(RestConfig.isCors()){
            CrossOriginResourceSharingFilter crossFilter=new CrossOriginResourceSharingFilter();
            crossFilter.setAllowOrigins(RestConfig.getCorsOriginList());
            providerList.add(crossFilter);
        }
    }
}
